;(function(global) {
  global.order = {
    template: `
              <div class="order-container">
                <div class="order-num">订单号：{{ order.ordernumber }}</div>
                <div class="order-coursetitle">
                  <p class="text-ess-1">{{ order.coursetitle }}</p>
                  <div class="order-commodity-type">{{ order.coursetype===2 ? '题库' : '课程' }}</div>
                </div>
                <div class="flex-wrap flex-justify-between flex-center-wrap order-commodity-info">
                  <div>共计: 1</div>
                  <div class="order-price-info">订单总价：￥<span class="order-total-price">{{ order.needpay }}</span></div>
                </div>
                <div class="flex-wrap flex-justify-between flex-center-wrap order-bottom">
                    <!-- 订单的状态 -->
                    <div v-if="order.state===1" class="order-wait-pay">待支付</div>
                    <div v-if="order.state===9" class="order-has-pay">已激活</div>
                    <div v-if="order.isgift===1" class="order-gift">赠送</div>
                    <div v-if="order.state===2" class="order-expired">已过期</div>
                    <div v-if="refund"></div>
                    <div>
                        <van-button round type="warning" v-if="order.state===1" class="order-for-help" @click="goToService(order.cateid)">咨询客服</van-button>
                        <van-button round type="danger" v-if="order.state===1" @click="goToPay(order.orderid)">去支付</van-button>
                        <van-button round type="warning" v-if="goToKe" @click="goListenCourse(order.courseid)">进入听课</van-button>
                        <van-button round type="warning" v-if="goToTest" @click="goDoExam">进入做题</van-button>
                        <van-button round type="warning" v-if="goToComment" @click="goTocomment">评价</van-button>
                        <van-button round type="warning" v-if="hasRecomment" color="rgb(200,200,200)">已评价</van-button>
                        <van-button round class="delete"  v-if="dele" @click="deleteItem(order.orderid)">删除</van-button>
                        <van-button round type="warning" v-if="refund" color="rgb(200,200,200)">已退款</van-button>
                    </div>
                </div>
              </div>
          `,
    props: ['order', 'index'],
    data() {
      return {
        // 订单状态 0为未支付 1为已激活 2为赠送
        orderStatus: 0,
        // 商品类型 0为课程 1为题库
        commodityType: 1
      }
    },
    computed: {
      goToKe() {
        if (
          (this.order.state === 9 || this.order.isgift === 1) &&
          this.order.coursetype === 1
        ) {
          return true
        } else {
          return false
        }
      },
      goToTest() {
        if (
          (this.order.state === 9 || this.order.isgift === 1) &&
          this.order.coursetype === 2
        ) {
          return true
        } else {
          return false
        }
      },
      goToComment() {
        if (
          (this.order.state === 9 || this.order.isgift === 1) &&
          this.order.iscomment === 0
        ) {
          return true
        } else {
          return false
        }
      },
      hasRecomment() {
        if (
          (this.order.state === 9 || this.order.isgift === 1) &&
          this.order.iscomment === 1
        ) {
          return true
        } else {
          return false
        }
      },
      refund() {
        if (this.order.state === 5) {
          return true
        } else {
          return false
        }
      },
      dele() {
        if (this.order.state === 1 || this.order.isgift === 1) {
          return true
        } else {
          return false
        }
      }
    },
    methods: {
      //  评价
      goTocomment() {
        location.href = `./comment.html?courseid=${this.order.courseid}&&orderid=${this.order.orderid}&&from=order`
      },
      // 删除
      deleteItem(orderid) {
        // TODO: 请求服务器进行删除
        this.$dialog
          .confirm({
            message: '确认删除此订单？'
          })
          .then(() => {
            // on confirm
            userService.removeOrder({ orderid: orderid }).then(res => {
              if (!res.code) {
                this.$emit('delete-item', this.index)
              } else {
                this.$toast.fail('删除失败')
              }
            })
          })
          .catch(() => {
            // on cancel
          })
      },
      // 支付
      goToPay(orderid) {
        window.location.href = `./pay.html?orderid=${orderid}`
      },
      // 进入听课
      goListenCourse(courseid) {
        location.href = `../courseCenter/videoCourse.html?courseid=${courseid}`
      },
      // 进入做题
      goDoExam() {
        location.href = `../examCenter/topic.html`
      },
      // 联系客服
      goToService(cateid) {
        location.href = `./customerService.html?cateid=${cateid}`
      }
    },
    created() {},
    mounted() {}
  }
})(window)
